<?php

/**
 * site actions.
 *
 * @package    sf_sandbox
 * @subpackage site
 * @author     arcefede
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z arcefede $
 */
class siteActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {

        if(sfContext::getInstance()->getRouting()->getCurrentRouteName() == 'homepage'
            || sfContext::getInstance()->getRouting()->getCurrentRouteName() == 'localized_homepage')
            $this->getUser()->setAttribute('route' , 'homepage');

        if (!$request->getParameter('sf_culture')) {
            if ($this->getUser()->isFirstRequest()) {
                $culture = $request->getPreferredCulture(array('en', 'es'));
                $this->getUser()->setCulture($culture);
                $this->getUser()->isFirstRequest(false);
            } else {
                $culture = $this->getUser()->getCulture();
            }

            $this->redirect('localized_homepage');
        }
    }

    public function executeContact(sfWebRequest $request) {

        if(sfContext::getInstance()->getRouting()->getCurrentRouteName() == 'contact')
            $this->getUser()->setAttribute('route' , 'contact');

        $this->form = new ContactForm();

        if ($request->isMethod('post')) {
            $this->form->bind($request->getParameter('contact'), $request->getFiles('contact'));
            if ($this->form->isValid()) {
                // send an email
                $contact = $request->getParameter('contact');
                $message = $this->getMailer()->compose(
                        $contact['email'], sfConfig::get('app_contact_email'), $this->getContext()->getI18N()->__('Contact Request'), $contact['message']
                );

                $this->getMailer()->send($message);
                $this->redirect('site/thankyou?' . http_build_query($request->getParameter('contact')));
            }
        }
    }

    public function executeThankyou(sfWebRequest $request) {
        $this->name = $request->getParameter('name');
    }

    public function executeWho(sfWebRequest $request) {
        if(sfContext::getInstance()->getRouting()->getCurrentRouteName() == 'who'){
            $this->getUser()->setAttribute('route' , 'who');
        }
    }

    public function executeClients(sfWebRequest $request) {
        if(sfContext::getInstance()->getRouting()->getCurrentRouteName() == 'clients'){
            $this->getUser()->setAttribute('route' , 'clients');
        }
    }

    public function executeSetLanguage(sfWebRequest $request) {
        $this->getUser()->setCulture($request->getParameter('sf_culture'));
        return $this->redirect('localized_homepage');
    }

}
